package org.flowers.unbeaten.config;

import lombok.extern.slf4j.Slf4j;
import org.flowers.unbeaten.config.proprety.SwaggerProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @description Swagger配置
 * @author RyanWang
 * @date 2021-08-02 14:06:30
 * @version 1.0
 */
@Slf4j
@Configuration
@EnableSwagger2
@ConditionalOnClass(Contact.class)
@EnableConfigurationProperties(SwaggerProperty.class)
public class SwaggerConfiguration {

    @Autowired
    private SwaggerProperty swaggerProperty;

    @Bean
    public Docket docker() {
        log.info("Read document configuration information");
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName(swaggerProperty.getGroupName())
                .enable(swaggerProperty.getEnable())
                .select()
                .apis(RequestHandlerSelectors.basePackage(swaggerProperty.getScanPackage()))
                .build()
                // 统一授权
                .securitySchemes(unifiedAuth())
                // 安全上下文
                .securityContexts(securityContexts());
    }

    /**
     * 统一授权
     * @return
     */
    private List<ApiKey> unifiedAuth() {
        List<ApiKey> list = new ArrayList<ApiKey>();
        list.add(new ApiKey("pc-type", "pc-type", "header"));
        list.add(new ApiKey("authorization", "authorization", "header"));
        list.add(new ApiKey("refresh_token", "refresh_token", "header"));
        return list;
    }

    /**
     * 定义在request请求header参数内不加入token参数
     * @return
     */
    private List<SecurityContext> securityContexts() {
        SecurityContext securityContext = SecurityContext
                .builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("^(?!auth).*$"))
                .build();
        List<SecurityContext> list = new ArrayList<SecurityContext>();
        list.add(securityContext);
        return list;
    }

    /**
     * 默认授权
     * @return
     */
    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        SecurityReference securityReference = new SecurityReference("authorization", authorizationScopes);
        List<SecurityReference> list = new ArrayList<SecurityReference>();
        list.add(securityReference);
        return list;
    }

    /**
     * API相关信息
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfo(
                swaggerProperty.getTitle(),
                swaggerProperty.getDescribe(),
                swaggerProperty.getVersion(),
                swaggerProperty.getTermsOfServiceUrl(), swaggerProperty.getContact(), swaggerProperty.getLicence(), swaggerProperty.getLicenceUrl(),
                new ArrayList<>()
        );
    }

}
